package com.example.shutdown;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.litepal.crud.DataSupport;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.TimePicker;

/**
 * @author jackly.zhao
 * 2119743875@qq.com
 * 2011-5-23 12:15:56
 */
public class MainActivity extends Activity implements OnClickListener,
		OnCheckedChangeListener {

	private final static String TAG = "yu_MainActivity";
	private final static String POWER_MANAGER_ENABLE = "power_enable";
	private final static String IS_EVERYDAY = "is_everyday";
	
	private final static String REBOOT_ENABLE = "reboot_enable";
	private final static String REBOOT_HOUR = "reboot_hour";
	private final static String REBOOT_MIN = "reboot_min";
	
	private final static String KEY_SHUT_HOUR = "offHour";
	private final static String KEY_SHUT_MIN = "offMin";
	private final static String KEY_BOOT_HOUR = "onHour";
	private final static String KEY_BOOT_MIN = "onMin";
	private final static String KEY_POWER_ON_ENABLE = "powerOnEnable";
	private final static String KEY_POWER_OFF_ENABLE = "powerOffEnable";
	private final static String SUNDAY = "1";
	private final static String MONDAY = "2";
	private final static String TUESDAY = "3";
	private final static String WEDNESDAY = "4";
	private final static String THURSDAY = "5";
	private final static String FRIDAY = "6";
	private final static String SATURDAY = "7";
	private final static int TYPE_SHUT_TIME = 0;
	private final static int TYPE_BOOT_TIME = 1;
	private final static int TYPE_REBOOT_TIME = 2;
	private final static int TYPE_EVERYDAY_SHUT_TIME = 3;
	private final static int TYPE_EVERYDAY_BOOT_TIME = 4;
	private final static int TYPE_WORKDAY_SHUT_TIME = 5;
	private final static int TYPE_WORKDAY_BOOT_TIME = 6;

	private SharedPreferences sharedPreferences;

	private Switch powerManagerSwitch;
	private RadioGroup radioGroup;
	private RadioButton everyDayButton;
	private RadioButton workDayButton;
	private TextView setShutTime;
	private TextView setBootTime;
	private boolean isEveryday;
	private boolean isPowerManagerEnable;

	private Switch rebootSwitch;
	private TextView setRebootTime;
	private boolean rebootEnable;
	private int rebootHour;
	private int rebootMin;

	private Switch[] everyDaySwitchs = new Switch[7];
	private int[] everyDaySwitchIds = { R.id.mon_switch, R.id.tue_switch,
			R.id.wed_switch, R.id.thu_switch, R.id.fri_switch, R.id.sat_switch,
			R.id.sun_switch };
	
	private Switch[] everyDayOffSwitchs = new Switch[7];
	private int[] everyDayOffSwitchIds = { R.id.mon_off_switch, R.id.tue_off_switch,
			R.id.wed_off_switch, R.id.thu_off_switch, R.id.fri_off_switch, R.id.sat_off_switch,
			R.id.sun_off_switch };

	private LinearLayout powerManager;
	private LinearLayout satLayout;
	private LinearLayout sunLayout;

	private TextView[] everyDayBootTimeTextViews = new TextView[7];
	private TextView[] everyDayShutTimeTextViews = new TextView[7];
	private TextView[] workDayBootTimeTextViews = new TextView[5];
	private TextView[] workDayShutTimeTextViews = new TextView[5];

	private int[] bootTimeTextViewIds = { R.id.mon_power_on, R.id.tue_power_on,
			R.id.wed_power_on, R.id.thu_power_on, R.id.fri_power_on,
			R.id.sat_power_on, R.id.sun_power_on };
	private int[] shutTimeTextViewIds = { R.id.mon_power_off,
			R.id.tue_power_off, R.id.wed_power_off, R.id.thu_power_off,
			R.id.fri_power_off, R.id.sat_power_off, R.id.sun_power_off };

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		initView();
	}

	private void initView() {
		sharedPreferences = getSharedPreferences("zhao", MODE_PRIVATE);
		boolean firstRun = sharedPreferences.getBoolean("first", true);
		if (firstRun) {
			List<PowerPlan> initPowerPlans = new ArrayList<PowerPlan>();
			for (int i = 1; i <= 7; i++) {
				initPowerPlans.add(new PowerPlan(false,false, i, 23, 59, 8, 30));
			}
			DataSupport.saveAll(initPowerPlans);
			firstRun = false;
			sharedPreferences.edit().putBoolean("first", firstRun).apply();
		}
		isEveryday = sharedPreferences.getBoolean(IS_EVERYDAY, true);
		Log.d(TAG,"----------initView-------------isEveryday = "+isEveryday);
		powerManagerSwitch = (Switch) findViewById(R.id.power_manager_switch);
		radioGroup = (RadioGroup) findViewById(R.id.radio_group);
		everyDayButton = (RadioButton) findViewById(R.id.every_day_button);
		workDayButton = (RadioButton) findViewById(R.id.work_day_button);
		setShutTime = (TextView) findViewById(R.id.set_shutdown_time);
		setBootTime = (TextView) findViewById(R.id.set_boot_time);
		setShutTime.setOnClickListener(this);
		setBootTime.setOnClickListener(this);

		rebootSwitch = (Switch) findViewById(R.id.reboot_switch);
		setRebootTime = (TextView) findViewById(R.id.reboot_time);
		setRebootTime.setOnClickListener(this);

		for (int i = 0; i < 7; i++) {
			everyDaySwitchs[i] = (Switch) findViewById(everyDaySwitchIds[i]);
			everyDaySwitchs[i].setOnCheckedChangeListener(this);
			everyDayOffSwitchs[i] = (Switch) findViewById(everyDayOffSwitchIds[i]);
			everyDayOffSwitchs[i].setOnCheckedChangeListener(this);
			everyDayBootTimeTextViews[i] = (TextView) findViewById(bootTimeTextViewIds[i]);
			everyDayBootTimeTextViews[i].setOnClickListener(this);
			everyDayShutTimeTextViews[i] = (TextView) findViewById(shutTimeTextViewIds[i]);
			everyDayShutTimeTextViews[i].setOnClickListener(this);

			if (i < 5) {
				workDayBootTimeTextViews[i] = everyDayBootTimeTextViews[i];
				workDayShutTimeTextViews[i] = everyDayShutTimeTextViews[i];
			}
		}

		powerManager = (LinearLayout) findViewById(R.id.power_manager);
		satLayout = (LinearLayout) findViewById(R.id.sat_power_manager);
		sunLayout = (LinearLayout) findViewById(R.id.sun_power_manager);
		
		everyDayButton.setChecked(isEveryday);
		workDayButton.setChecked(!isEveryday);
		
		powerManagerSwitch
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						isPowerManagerEnable = isChecked;
						Log.d(TAG, "---powerManagerSwitch----" + isPowerManagerEnable);
						
						powerManager.setVisibility(isPowerManagerEnable ? View.VISIBLE
								: View.GONE);
						radioGroup.setVisibility(isPowerManagerEnable ? View.VISIBLE
								: View.GONE);
						setBootTime.setVisibility(isPowerManagerEnable ? View.VISIBLE
								: View.GONE);
						setShutTime.setVisibility(isPowerManagerEnable ? View.VISIBLE
								: View.GONE);
						
						sharedPreferences.edit()
								.putBoolean(POWER_MANAGER_ENABLE, isPowerManagerEnable)
								.apply();
						if(isEveryday){
							for (int i = 1; i <= 7; i++) {
								updatePowerOnEnable(isPowerManagerEnable, String.valueOf(i));//update databases
								updatePowerOffEnable(isPowerManagerEnable, String.valueOf(i));
							}
						}else{
							for (int i = 1; i <= 7; i++) {
								if(i == 1 || i == 7){
									updatePowerOnEnable(false, String.valueOf(i));//update databases
									updatePowerOffEnable(false, String.valueOf(i));
								}else{
									updatePowerOnEnable(isPowerManagerEnable, String.valueOf(i));//update databases
									updatePowerOffEnable(isPowerManagerEnable, String.valueOf(i));
								}
							}
						}
						
						// update UI
						Log.d(TAG,"----------PowerManagerSwitch Changed then ^^^ Search Databases for update UI And start Service!-----------");
						updatePowerManagerShow();
						startAlarmService();
						
					}
				});

		everyDayButton
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						isEveryday = isChecked;
						sharedPreferences.edit()
								.putBoolean(IS_EVERYDAY, isEveryday).apply();
						
						if(isEveryday){
							for (int i = 1; i <= 7; i++) {
								updatePowerOnEnable(isPowerManagerEnable, String.valueOf(i));//update databases
								updatePowerOffEnable(isPowerManagerEnable, String.valueOf(i));
							}
						}else{
							for (int i = 1; i <= 7; i++) {
								if(i == 1 || i == 7){
									updatePowerOnEnable(false, String.valueOf(i));//update databases
									updatePowerOffEnable(false, String.valueOf(i));
								}else{
									updatePowerOnEnable(isPowerManagerEnable, String.valueOf(i));//update databases
									updatePowerOffEnable(isPowerManagerEnable, String.valueOf(i));
								}
							}
						}
						Log.d(TAG,"----------EveryDayButton changed then Search Databases for Update UI And start Service!-----------");
						updatePowerManagerShow();
						startAlarmService();
						
					}
				});
				
		
		
		
		rebootSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				rebootEnable = isChecked;
				Log.d(TAG, "---rebootEnable--" + rebootEnable);
				sharedPreferences.edit()
						.putBoolean(REBOOT_ENABLE, rebootEnable).apply();
				setRebootTime.setEnabled(rebootEnable);
				// startService
				startAlarmService();
				Log.d(TAG,"----------rebootSwitch changed then start Service!-----------");
			}
		});
	
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		Log.d(TAG,"------onStart Search Databases for Update UI!!!--------");
		isPowerManagerEnable = sharedPreferences.getBoolean(POWER_MANAGER_ENABLE, false);
		if(isPowerManagerEnable){
			startAlarmService();
		}
		
		Log.d(TAG,"----------initView-------------isPowerManagerEnable = "+isPowerManagerEnable);
		powerManagerSwitch.setChecked(isPowerManagerEnable);
		powerManager.setVisibility(isPowerManagerEnable ? View.VISIBLE
								: View.GONE);
						radioGroup.setVisibility(isPowerManagerEnable ? View.VISIBLE
								: View.GONE);
						setBootTime.setVisibility(isPowerManagerEnable ? View.VISIBLE
								: View.GONE);
						setShutTime.setVisibility(isPowerManagerEnable ? View.VISIBLE
								: View.GONE);
		
		rebootEnable = sharedPreferences.getBoolean(REBOOT_ENABLE, false);
		rebootHour = sharedPreferences.getInt(REBOOT_HOUR, 22);
		rebootMin = sharedPreferences.getInt(REBOOT_MIN, 0);
		setRebootTime.setText(formatTime(rebootHour, rebootMin,TYPE_REBOOT_TIME));
		setRebootTime.setEnabled(rebootEnable);
		rebootSwitch.setChecked(rebootEnable);
		updatePowerManagerShow();
	}

	private void updatePowerManagerShow() {
		List<PowerPlan> powerPlans = DataSupport.findAll(PowerPlan.class);
		for (PowerPlan powerPlan : powerPlans) {
			int week = powerPlan.getWeek();
			int offHour = powerPlan.getOffHour();
			int offMin = powerPlan.getOffMin();
			int onHour = powerPlan.getOnHour();
			int onMin = powerPlan.getOnMin();
			boolean powerOnEnable = powerPlan.isPowerOnEnable();
			boolean powerOffEnable = powerPlan.isPowerOffEnable();
			Log.d(TAG, "--week:" + week + "--bootTime = " + onHour + ":"
					+ onMin + "--shutTime = " + offHour + ":" + offMin
					+ "--powerOnEnable = " + powerOnEnable + "---powerOffEnable = " + powerOffEnable);
			switch (week) {

			case Calendar.MONDAY:
				everyDaySwitchs[0].setChecked(powerOnEnable);
				everyDayOffSwitchs[0].setChecked(powerOffEnable);
				everyDayBootTimeTextViews[0].setText(formatTime(onHour, onMin,
						TYPE_BOOT_TIME));
				everyDayShutTimeTextViews[0].setText(formatTime(offHour,
						offMin, TYPE_SHUT_TIME));
				break;
			case Calendar.TUESDAY:
				everyDaySwitchs[1].setChecked(powerOnEnable);
				everyDayOffSwitchs[1].setChecked(powerOffEnable);
				everyDayBootTimeTextViews[1].setText(formatTime(onHour, onMin,
						TYPE_BOOT_TIME));
				everyDayShutTimeTextViews[1].setText(formatTime(offHour,
						offMin, TYPE_SHUT_TIME));
				break;
			case Calendar.WEDNESDAY:
				everyDaySwitchs[2].setChecked(powerOnEnable);
				everyDayOffSwitchs[2].setChecked(powerOffEnable);
				everyDayBootTimeTextViews[2].setText(formatTime(onHour, onMin,
						TYPE_BOOT_TIME));
				everyDayShutTimeTextViews[2].setText(formatTime(offHour,
						offMin, TYPE_SHUT_TIME));
				break;
			case Calendar.THURSDAY:
				everyDaySwitchs[3].setChecked(powerOnEnable);
				everyDayOffSwitchs[3].setChecked(powerOffEnable);
				everyDayBootTimeTextViews[3].setText(formatTime(onHour, onMin,
						TYPE_BOOT_TIME));
				everyDayShutTimeTextViews[3].setText(formatTime(offHour,
						offMin, TYPE_SHUT_TIME));
				break;
			case Calendar.FRIDAY:
				everyDaySwitchs[4].setChecked(powerOnEnable);
				everyDayOffSwitchs[4].setChecked(powerOffEnable);
				everyDayBootTimeTextViews[4].setText(formatTime(onHour, onMin,
						TYPE_BOOT_TIME));
				everyDayShutTimeTextViews[4].setText(formatTime(offHour,
						offMin, TYPE_SHUT_TIME));
				break;
			case Calendar.SATURDAY:
				everyDaySwitchs[5].setChecked(powerOnEnable);
				everyDayOffSwitchs[5].setChecked(powerOffEnable);
				everyDayBootTimeTextViews[5].setText(formatTime(onHour, onMin,
						TYPE_BOOT_TIME));
				everyDayShutTimeTextViews[5].setText(formatTime(offHour,
						offMin, TYPE_SHUT_TIME));
				break;
			case Calendar.SUNDAY:
				everyDaySwitchs[6].setChecked(powerOnEnable);
				everyDayOffSwitchs[6].setChecked(powerOffEnable);
				everyDayBootTimeTextViews[6].setText(formatTime(onHour, onMin,
						TYPE_BOOT_TIME));
				everyDayShutTimeTextViews[6].setText(formatTime(offHour,
						offMin, TYPE_SHUT_TIME));
				break;
			}
		}
	}
	
	private void startAlarmService(){
		startService(new Intent(MainActivity.this, AlarmService.class));
	}

	private void updatePowerOnEnable(boolean powerOnEnable, String week) {
		PowerPlan powerPlan = new PowerPlan();
		if (powerOnEnable) {
			powerPlan.setPowerOnEnable(powerOnEnable);
		} else {
			powerPlan.setToDefault(KEY_POWER_ON_ENABLE);
		}
		powerPlan.updateAll("week = ?", week);
	}
	
	private void updatePowerOffEnable(boolean powerOffEnable, String week) {
		PowerPlan powerPlan = new PowerPlan();
		if (powerOffEnable) {
			powerPlan.setPowerOffEnable(powerOffEnable);
		} else {
			powerPlan.setToDefault(KEY_POWER_OFF_ENABLE);
		}
		powerPlan.updateAll("week = ?", week);
	}

	private void updateDatabases(String key1, String value1, String key2,
			String value2, String week) {
		ContentValues values = new ContentValues();
		values.put(key1, value1);
		values.put(key2, value2);
		DataSupport.updateAll(PowerPlan.class, values, "week = ?", week);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.mon_power_on:
			showTimePicker(everyDayBootTimeTextViews[0], TYPE_BOOT_TIME, MONDAY);
			break;
		case R.id.tue_power_on:
			showTimePicker(everyDayBootTimeTextViews[1], TYPE_BOOT_TIME,
					TUESDAY);
			break;
		case R.id.wed_power_on:
			showTimePicker(everyDayBootTimeTextViews[2], TYPE_BOOT_TIME,
					WEDNESDAY);
			break;
		case R.id.thu_power_on:
			showTimePicker(everyDayBootTimeTextViews[3], TYPE_BOOT_TIME,
					THURSDAY);
			break;
		case R.id.fri_power_on:
			showTimePicker(everyDayBootTimeTextViews[4], TYPE_BOOT_TIME, FRIDAY);
			break;
		case R.id.sat_power_on:
			showTimePicker(everyDayBootTimeTextViews[5], TYPE_BOOT_TIME,
					SATURDAY);
			break;
		case R.id.sun_power_on:
			showTimePicker(everyDayBootTimeTextViews[6], TYPE_BOOT_TIME, SUNDAY);
			break;

		case R.id.mon_power_off:
			showTimePicker(everyDayShutTimeTextViews[0], TYPE_SHUT_TIME, MONDAY);
			break;
		case R.id.tue_power_off:
			showTimePicker(everyDayShutTimeTextViews[1], TYPE_SHUT_TIME,
					TUESDAY);
			break;
		case R.id.wed_power_off:
			showTimePicker(everyDayShutTimeTextViews[2], TYPE_SHUT_TIME,
					WEDNESDAY);
			break;
		case R.id.thu_power_off:
			showTimePicker(everyDayShutTimeTextViews[3], TYPE_SHUT_TIME,
					THURSDAY);
			break;
		case R.id.fri_power_off:
			showTimePicker(everyDayShutTimeTextViews[4], TYPE_SHUT_TIME, FRIDAY);
			break;
		case R.id.sat_power_off:
			showTimePicker(everyDayShutTimeTextViews[5], TYPE_SHUT_TIME,
					SATURDAY);
			break;
		case R.id.sun_power_off:
			showTimePicker(everyDayShutTimeTextViews[6], TYPE_SHUT_TIME, SUNDAY);
			break;
		case R.id.reboot_time:
			showTimePicker(setRebootTime, TYPE_REBOOT_TIME, null);
			break;

		case R.id.set_boot_time:
			if (isEveryday) {
				showTimePicker(null, TYPE_EVERYDAY_BOOT_TIME, null);
			} else {
				showTimePicker(null, TYPE_WORKDAY_BOOT_TIME, null);
			}
			break;

		case R.id.set_shutdown_time:
			if (isEveryday) {
				showTimePicker(null, TYPE_EVERYDAY_SHUT_TIME, null);
			} else {
				showTimePicker(null, TYPE_WORKDAY_SHUT_TIME, null);
			}
			break;
		}
	}

	private void showTimePicker(final TextView textView, final int timeType,
			final String week) {
		TimePickerDialog mTimePickerDialog = new TimePickerDialog(this,
				AlertDialog.THEME_HOLO_LIGHT,
				new TimePickerDialog.OnTimeSetListener() {
					@Override
					public void onTimeSet(TimePicker view, int hourOfDay,
							int minute) {
						if (null != textView) {
							textView.setText(formatTime(hourOfDay, minute,
									timeType));
						}
						if (timeType == TYPE_BOOT_TIME) {
							updateDatabases(KEY_BOOT_HOUR,
									String.valueOf(hourOfDay), KEY_BOOT_MIN,
									String.valueOf(minute), week);
						} else if (timeType == TYPE_SHUT_TIME) {
							updateDatabases(KEY_SHUT_HOUR,
									String.valueOf(hourOfDay), KEY_SHUT_MIN,
									String.valueOf(minute), week);
						} else if (timeType == TYPE_REBOOT_TIME) {
							Editor editor = sharedPreferences.edit();
							editor.putInt(REBOOT_HOUR, hourOfDay);
							editor.putInt(REBOOT_MIN, minute);
							editor.apply();

						} else if (timeType == TYPE_EVERYDAY_BOOT_TIME) {
							for (int i = 0; i < 7; i++) {
								everyDayBootTimeTextViews[i]
										.setText(formatTime(hourOfDay, minute,
												TYPE_BOOT_TIME));
								updateDatabases(KEY_BOOT_HOUR,
										String.valueOf(hourOfDay),
										KEY_BOOT_MIN, String.valueOf(minute),
										String.valueOf(i + 1));
							}

						} else if (timeType == TYPE_EVERYDAY_SHUT_TIME) {
							for (int i = 0; i < 7; i++) {
								everyDayShutTimeTextViews[i]
										.setText(formatTime(hourOfDay, minute,
												TYPE_SHUT_TIME));
								updateDatabases(KEY_SHUT_HOUR,
										String.valueOf(hourOfDay),
										KEY_SHUT_MIN, String.valueOf(minute),
										String.valueOf(i + 1));
							}
						} else if (timeType == TYPE_WORKDAY_BOOT_TIME) {
							for (int i = 0; i < 5; i++) {
								workDayBootTimeTextViews[i].setText(formatTime(
										hourOfDay, minute, TYPE_BOOT_TIME));
								updateDatabases(KEY_BOOT_HOUR,
										String.valueOf(hourOfDay),
										KEY_BOOT_MIN, String.valueOf(minute),
										String.valueOf(i + 2));
							}

						} else if (timeType == TYPE_WORKDAY_SHUT_TIME) {
							for (int i = 0; i < 5; i++) {
								workDayShutTimeTextViews[i].setText(formatTime(
										hourOfDay, minute, TYPE_SHUT_TIME));
								updateDatabases(KEY_SHUT_HOUR,
										String.valueOf(hourOfDay),
										KEY_SHUT_MIN, String.valueOf(minute),
										String.valueOf(i + 2));
							}
						}
						Log.d(TAG,
								"--------------Time Set Then update Databases update UI And start Service!-----------");
						startAlarmService();
					}
				}, 0, 0, true);

		switch (timeType) {
		case TYPE_BOOT_TIME:
			mTimePickerDialog.setTitle(R.string.boot_time);
			break;

		case TYPE_EVERYDAY_BOOT_TIME:
			mTimePickerDialog.setTitle(R.string.everyday_boot_time);
			break;

		case TYPE_WORKDAY_BOOT_TIME:
			mTimePickerDialog.setTitle(R.string.workday_boot_time);
			break;

		case TYPE_SHUT_TIME:
			mTimePickerDialog.setTitle(R.string.shut_time);
			break;
			
		case TYPE_EVERYDAY_SHUT_TIME:
			mTimePickerDialog.setTitle(R.string.everyday_shut_time);
			break;
			
		case TYPE_WORKDAY_SHUT_TIME:
			mTimePickerDialog.setTitle(R.string.workday_shut_time);
			break;
			
		case TYPE_REBOOT_TIME:
			mTimePickerDialog.setTitle(R.string.set_reboot_time);
			break;
		}
		
		mTimePickerDialog.show();
	}

	private String formatTime(int h, int m, int timeType) {
		StringBuffer buf = new StringBuffer();
		if (h < 10) {
			buf.append("0" + h);
		} else {
			buf.append(h);
		}
		buf.append(" : ");
		if (m < 10) {
			buf.append("0" + m);
		} else {
			buf.append(m);
		}
		if (timeType == TYPE_BOOT_TIME) {
			return String.format(getResources().getString(R.string.power_on),
					buf.toString());
		} else if (timeType == TYPE_SHUT_TIME) {
			return String.format(getResources().getString(R.string.power_off),
					buf.toString());
		} else if (timeType == TYPE_REBOOT_TIME) {
			return String.format(
					getResources().getString(R.string.reboot_time),
					buf.toString());
		} else {
			return "";
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		switch (buttonView.getId()) {
		case R.id.mon_switch:
			Log.d(TAG, "--monSwitch--" + isChecked + "-----monSwitch changed Then updateDatabases And start Service!");
			everyDayBootTimeTextViews[0].setEnabled(isChecked);
			updatePowerOnEnable(isChecked, MONDAY);
			startAlarmService();
			break;

		case R.id.tue_switch:
			Log.d(TAG, "--tueSwitch--" + isChecked + "-----tueSwitch changed Then updateDatabases And start Service!");
			everyDayBootTimeTextViews[1].setEnabled(isChecked);
			updatePowerOnEnable(isChecked, TUESDAY);
			startAlarmService();
			break;

		case R.id.wed_switch:
			Log.d(TAG, "--wedSwitch--" + isChecked + "-----wedSwitch changed Then updateDatabases And start Service!");
			everyDayBootTimeTextViews[2].setEnabled(isChecked);
			updatePowerOnEnable(isChecked, WEDNESDAY);
			startAlarmService();
			break;

		case R.id.thu_switch:
			Log.d(TAG, "--thuSwitch--" + isChecked + "-----thuSwitch changed Then updateDatabases And start Service!");
			everyDayBootTimeTextViews[3].setEnabled(isChecked);
			updatePowerOnEnable(isChecked, THURSDAY);
			startAlarmService();
			break;
			
		case R.id.fri_switch:
			Log.d(TAG, "--friSwitch--" + isChecked + "-----friSwitch changed Then updateDatabases And start Service!");
			everyDayBootTimeTextViews[4].setEnabled(isChecked);
			updatePowerOnEnable(isChecked, FRIDAY);
			startAlarmService();
			break;
			
		case R.id.sat_switch:
			Log.d(TAG, "--satSwitch--" + isChecked + "-----satSwitch changed Then updateDatabases And start Service!");
			everyDayBootTimeTextViews[5].setEnabled(isChecked);
			updatePowerOnEnable(isChecked, SATURDAY);
			startAlarmService();
			break;
			
		case R.id.sun_switch:
			Log.d(TAG, "--sunSwitch--" + isChecked + "-----sunSwitch changed Then updateDatabases And start Service!");
			everyDayBootTimeTextViews[6].setEnabled(isChecked);
			updatePowerOnEnable(isChecked, SUNDAY);
			startAlarmService();
			break;
			
		case R.id.mon_off_switch:
			Log.d(TAG, "--monSwitch--" + isChecked + "-----monSwitch changed Then updateDatabases And start Service!");
			everyDayShutTimeTextViews[0].setEnabled(isChecked);
			updatePowerOffEnable(isChecked, MONDAY);
			startAlarmService();
			break;

		case R.id.tue_off_switch:
			Log.d(TAG, "--tueSwitch--" + isChecked + "-----tueSwitch changed Then updateDatabases And start Service!");
			everyDayShutTimeTextViews[1].setEnabled(isChecked);
			updatePowerOffEnable(isChecked, TUESDAY);
			startAlarmService();
			break;

		case R.id.wed_off_switch:
			Log.d(TAG, "--wedSwitch--" + isChecked + "-----wedSwitch changed Then updateDatabases And start Service!");
			everyDayShutTimeTextViews[2].setEnabled(isChecked);
			updatePowerOffEnable(isChecked, WEDNESDAY);
			startAlarmService();
			break;

		case R.id.thu_off_switch:
			Log.d(TAG, "--thuSwitch--" + isChecked + "-----thuSwitch changed Then updateDatabases And start Service!");
			everyDayShutTimeTextViews[3].setEnabled(isChecked);
			updatePowerOffEnable(isChecked, THURSDAY);
			startAlarmService();
			break;
			
		case R.id.fri_off_switch:
			Log.d(TAG, "--friSwitch--" + isChecked + "-----friSwitch changed Then updateDatabases And start Service!");
			everyDayShutTimeTextViews[4].setEnabled(isChecked);
			updatePowerOffEnable(isChecked, FRIDAY);
			startAlarmService();
			break;
			
		case R.id.sat_off_switch:
			Log.d(TAG, "--satSwitch--" + isChecked + "-----satSwitch changed Then updateDatabases And start Service!");
			everyDayShutTimeTextViews[5].setEnabled(isChecked);
			updatePowerOffEnable(isChecked, SATURDAY);
			startAlarmService();
			break;
			
		case R.id.sun_off_switch:
			Log.d(TAG, "--sunSwitch--" + isChecked + "-----sunSwitch changed Then updateDatabases And start Service!");
			everyDayShutTimeTextViews[6].setEnabled(isChecked);
			updatePowerOffEnable(isChecked, SUNDAY);
			startAlarmService();
			break;
		}
	}

}
